package com.carl.leetcode.string;

/**
 * Created by carlXie on 2016/1/13.
 * email: kkrimen@gmail.com
 */
public class RomanToInteger {
    public static void main(String[] args) {
        System.out.println(romanToInt("MMCMCDLXLVIII"));
    }

    public static int romanToInt(String s) {
        int[] values = new int[]{1000, 500, 100, 50, 10, 5, 1};
        String[] symbols = new String[]{"M", "D", "C", "L", "X", "V", "I"};

        int result = 0;
        for (int i = 0, len = s.length(); i < len; i++) {
            if(i < len - 1 && charToInt(s.charAt(i)) < charToInt(s.charAt(i + 1)))
                result -= charToInt(s.charAt(i));
            else result += charToInt(s.charAt(i));
        }

        return result;
    }

    private static int charToInt(char c) {
        switch (c) {
            case 'M': return 1000;
            case 'D': return 500;
            case 'C': return 100;
            case 'L': return 50;
            case 'X': return 10;
            case 'V': return 5;
            case 'I': return 1;
            default: return 0;
        }
    }
}
